package com.cc.crm.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @program: 
 * @ClassName:
 * @description:
 * @author: zgc
 * @date: 
 * @Version 1.0
 **/
public class Interceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取 Session
        HttpSession session = request.getSession();
        // 检查 Session 中是否存在登录信息
        if (session.getAttribute("username") == null) {
            // 用户未登录，重定向到登录页面
            response.sendRedirect("/user/toLogin");
            return false; // 拦截请求
        }
        // 用户已登录，放行请求
        return true;
    }

//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }
}
